<?php
/***
* @version $Id: admin_policy.php 336 2007-01-23 08:12:56Z flexiondotorg $
* @copyright (c) 2006 - 2007 Flexion.Org
*            (c) 2004 - 2005 Project Minerva
*            (c) 2003 - 2004 Sko22 < sko22@quellicheilpc.com > http://www.quellicheilpc.com/
*            (c) 2001 - 2006 phpBB Group
* @license   http://opensource.org/licenses/gpl-license.php GNU Public License
***/

define('IN_R3BORN', true);

if ( !empty($setmodules) )
{
    $module['General']['Policy'] = basename(__FILE__);
    return;
}

//
// Load default header
//
$root_path = './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require('./pagestart.' . $phpEx);
include_once($root_path . './includes/functions_message.' . $phpEx);

$mode = get_var('mode', 'list');

switch ($mode)
{
    case 'save':

		$id = get_var('id', 0);
		$prev_mode = get_var('prev_mode', '');

	    //$policy_name = str_replace("\'", "''", $_POST['policy_name']);
		$policy_text = str_replace("\'", "''", $_POST['policy_text']);
		$policy_date = time();

		//
	    // Save the policy
	    //
		if ( $prev_mode == 'modify' )
		{
			$sql = "UPDATE " . POLICY_TABLE . "
				SET policy_text = '" . $policy_text . "', policy_date = '" . $policy_date . "'
				WHERE policy_id = '" . $id . "'";
		}
		else
		{
			$sql = "INSERT INTO " . POLICY_TABLE . " ( policy_id, policy_text, policy_date )
				VALUES ( '" . $id . "', '" . $policy_text . "', '" . $policy_date . "' )";
		}

        if ( !$db->sql_query($sql) )
        {
            message_die(GENERAL_ERROR, 'Could not create/modify policies', '', __LINE__, __FILE__, $sql);
        }

		//message_die(GENERAL_MESSAGE, 'Done!');

		$message = $lang['Policy_updated'] . "<br /><br />" . sprintf($lang['Click_return_policy'], "<a href=\"" . append_sid("admin_policy.$phpEx") . "\">", "</a>") . "<br /><br />" . sprintf($lang['Click_return_admin_index'], "<a href=\"" . append_sid("index.$phpEx?pane=right") . "\">", "</a>");
		message_die(GENERAL_MESSAGE, $message);

        break;

	case '':
	case 'list':

		$template->assign_block_vars('switch_list', array());

		$temp_url = $root_path . 'admin/' . basename(__FILE__);

		$u_create = "<a href='" . append_sid($temp_url . "?mode=create&amp;id=%s") . "'>" . $lang['Create'] . "</a>";
		$u_modify = "<a href='" . append_sid($temp_url . "?mode=modify&amp;id=%s") . "'>" . $lang['Modify'] . "</a>";

		$sql = "SELECT * FROM " . POLICY_TABLE . "
			ORDER BY policy_id";

		if( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not obtain the policies', '', __LINE__, __FILE__, $sql);
		}

		$policies = array();

		while ($row = $db->sql_fetchrow($result))
		{
			$policies[$row['policy_id']] = array(
				'NAME'	=> $row['policy_name'],
				'TEXT'	=> $row['policy_text'],
				'DATE'	=> $row['policy_date']
			);
		}

		$db->sql_freeresult($result);

		$template->assign_vars(array(
		    'L_POLICIES'     => $lang['Policies'],
			'L_REGISTRATION' => $lang['Registration'],
			'L_PRIVACY'	     => $lang['Privacy_Policy'],
			'L_TERMS'	     => $lang['Terms_of_Use'],
			'L_COPYRIGHT'	     => $lang['Copyright'],

			'U_REGISTRATION' => sprintf((isset($policies[REGISTRATION_POL_ID]) ? $u_modify : $u_create), REGISTRATION_POL_ID),
			'U_PRIVACY'	 => sprintf((isset($policies[PRIVACY_POL_ID]) ? $u_modify : $u_create), PRIVACY_POL_ID),
			'U_TERMS'	 => sprintf((isset($policies[TERMS_POL_ID]) ? $u_modify : $u_create), TERMS_POL_ID),
			'U_COPYRIGHT'	 => sprintf((isset($policies[COPYRIGHT_POL_ID]) ? $u_modify : $u_create), COPYRIGHT_POL_ID),
		));

		break;

	case 'create':
	case 'modify':

		$id = get_var('id', 0);

		$template->assign_block_vars('switch_modify', array());

		if ($mode == 'modify')
		{
		    //
		    // Get message of the policy from database.
		    //
		    $sql = "SELECT * FROM " . POLICY_TABLE . " WHERE policy_id = '" . $id . "'";

		    if( !($result = $db->sql_query($sql)) )
		    {
		        message_die(GENERAL_ERROR, 'Could not obtain the policies', '', __LINE__, __FILE__, $sql);
		    }

		    $row = $db->sql_fetchrow($result);
			$db->sql_freeresult($result);

			$policy_name = $row['policy_name'];
			$policy_text = $row['policy_text'];
		    $policy_date = create_date($lang['DATE_FORMAT'], $row['policy_date'], $config['board_timezone']);
		}
		else
		{
			$policy_name = '';
			$policy_text = '';
			$policy_date = 0;
		}

        $template->assign_vars(array(
			'L_LAST_MODIFIED'	=> $lang['last_modified'],
            'POLICY_DATE' => $policy_date,
            'L_DO' => $lang['Save'],
            'S_POLICY_TEXT' => trim($policy_text),
            'S_POLICY_ACTION' => append_sid($root_path . "admin/admin_policy.$phpEx?mode=save&amp;prev_mode=$mode&amp;id=$id")
            )
        );

        break;
}

$template->set_filenames(array(
    'body' => 'policy_config_body.tpl' )
);

$template->assign_vars(array(
    'L_POLICY' => $lang['Policy'],
    'L_POLICY_EXPLAIN' => $lang['Policy_explain']
));

$template->pparse('body');

page_footer_admin();

?>